package com.lsu.app.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lsu.app.bean.Praise;
import com.lsu.app.bean.Talk;
import com.lsu.app.config.target.RequestRoles;
import com.lsu.app.service.*;
import com.lsu.app.tool.R;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @author hjj
 * @date 2023/10/27 11:00
 */
@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/praises")
public class PraiseContraller {
    @Autowired
    private HttpServletRequest request;
    private final PraiseService service;
    private final UserService userService;
    private final CourseService courseService;
    private final ResourceService resourceService;
    private final CommentService commentService;
    private final TalkService talkService;


    public PraiseContraller(PraiseService service, UserService userService, CourseService courseService, ResourceService resourceService, CommentService commentService, TalkService talkService) {
        this.service = service;
        this.userService = userService;
        this.courseService = courseService;
        this.resourceService = resourceService;
        this.commentService = commentService;
        this.talkService = talkService;
    }

    @RequestRoles({"student","teacher"})
    @GetMapping("/set")
    @ApiOperation("学生点赞,再次点赞将会取消点赞[返回的类中type=0即取消点赞了]")
    public R<Praise> setPraise(
            @ApiParam("评论/教师/课程/课程视频/话题 -id")@RequestParam("id")Integer id,
            @ApiParam("类型（1评论，2教师，3课程，4课程视频,5话题）")@RequestParam("type")Integer type
    ){
        Integer userId = (Integer) request.getSession().getAttribute("id");
        Praise praise = service.getOneByUserIdAndTypeAndTargetId(userId,type,id);
        Integer teacherId = id;
        if(praise!=null){//存在点赞 则删除记录
            if(service.removeById(praise.getId())){
                //减去热度
                if(type==5){
                    talkService.updateTalkHeat(id,-1);
                }else if(type==3||type==4){
                    courseService.updateHeatById(id,-1);
                }else if(type==1){
                    commentService.addHeatById(id,-1);
                }
                return R.success(new Praise(0));
            }
            return R.err("请稍后再试");
        }else{//不存在则添加
            String typeName ="评论";
            String targetName="";
            if(type==1){
                targetName=commentService.getById(id).getContent();
                commentService.addHeatById(id,1);
                teacherId=commentService.getById(id).getTargetUserId();
            }else if(type==2){
                typeName="教师";
                targetName=userService.getById(id).getName();
            }else if(type==3){
                typeName="课程";
                targetName=courseService.getCourseNameById(id);
                courseService.updateHeatById(id,1);
                teacherId=courseService.getTeacherIdByCourseId(id);
            }else if(type==4){
                typeName="课程视频";
                targetName=resourceService.getById(id).getRemake();
                courseService.updateHeatById(id,1);
                teacherId=resourceService.getTeacherIdByVideoId(id);
            }else if(type==5){
                typeName="话题";
                targetName=talkService.getTitleById(id);
                talkService.updateTalkHeat(id,1);
                teacherId=talkService.getHostIdByTalkId(id);
            }else {
                return R.err("类型出错");
            }
            Praise newPraise = new Praise(teacherId,userId,userService.getById(userId).getName(),typeName,type,targetName,id, LocalDateTime.now().toString());
            service.save(newPraise);
            return R.success(newPraise);
        }
    }
    @RequestRoles({"student","teacher"})
    @GetMapping("/get")
    @ApiOperation("获取点赞数")
    public R<Integer> getPraiseNum(
            @ApiParam("评论/教师/课程/课程视频 -id")@RequestParam("id")Integer id,
            @ApiParam("类型（1评论，2教师，3课程，4课程视频）")@RequestParam("type")Integer type
    ){
        return R.success(service.getNumByTypeAndTargetId(type,id));
    }

}
